# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=creduce
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.5.0.r77.g56670fd
pkgrel=1
pkgdesc="A C program reducer (mingw-w64)"
depends=('perl-Benchmark-Timer'
         'perl-Exporter-Lite'
         'perl-File-Which'
         'perl-Getopt-Tabular'
         'perl-Regexp-Common'
         'perl-Sys-CPU'
         "${MINGW_PACKAGE_PREFIX}-astyle"
         "${MINGW_PACKAGE_PREFIX}-indent"
         "${MINGW_PACKAGE_PREFIX}-clang")
arch=('any')
url='https://github.com/csmith-project/creduce'
license=('custom:University of Illinois/NCSA Open Source License')
makedepends=("git" "flex" "${MINGW_PACKAGE_PREFIX}-clang")
source=(${_realname}::git+https://github.com/csmith-project/creduce.git
        "llvm-config-cygpath")
sha256sums=('SKIP'
            '03a7239d1e26d8a5dd5f405e10826679725cae6c01a9c37e80c6f1c184dad0a9')

pkgver() {
  cd "${srcdir}/${_realname}"
  git describe --long | sed 's/\([^-]*-g\)/r\1/;s/-/./g' | sed 's/creduce.//'
}

prepare() {
  cd "${srcdir}"
  [[ -d tools ]] && rm -rf tools
  mkdir tools
  cp -f "${srcdir}"/llvm-config-cygpath tools/llvm-config
}

build() {
  cd "${srcdir}"
  [ -d ${CARCH} ] && rm -rf ${CARCH}
  mkdir ${CARCH}
  cd ${CARCH}
  PATH="${srcdir}/tools:$PATH" \
  ../${_realname}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/${CARCH}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "../${_realname}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  # Point creduce perl script:
  # #!/${MINGW_PREFIX} -> #!/usr
  # ..  to msys2 perl.
  sed "s|#!${MINGW_PREFIX}|#!/usr|g" \
    -i ${pkgdir}${MINGW_PREFIX}/bin/creduce
}
